package com.industrial.premu.controller.chart;

import cn.hutool.core.collection.CollectionUtil;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.premu.dto.PreMuChartsDto;
import com.industrial.premu.service.IPreMuBenefitEvaluationService;
import com.industrial.premu.service.IPreMuBenfitEvaluationChatService;
import com.industrial.premu.util.PageUtils;
import com.industrial.premu.vo.PreMuChartsVo;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/chart/conomyAnalysis")
public class PreMuEconomyAnalysisController  extends BaseController {

    @Autowired
    private IPreMuBenefitEvaluationService preMuBenefitEvaluationService;
    @Autowired
    private IPreMuBenfitEvaluationChatService preMuBenfitEvaluationChatService;
    @GetMapping(value = "/economyData")
    public AjaxResult economyData(PreMuChartsDto preChartsDto) {
        List<PreMuChartsVo> chartsVoList=new ArrayList<>();
        int years=Integer.valueOf(preChartsDto.getYears());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
        }
        if(years>=2019){
            preChartsDto.setEndYears(String.valueOf(years));
            int  startYears=years-4;
            if(startYears>2019){
                preChartsDto.setStartYears(String.valueOf(startYears));
            }else{
                preChartsDto.setStartYears("2019");
            }
            preChartsDto.setYears("");
        }

        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
            List<PreMuChartsVo> cList=    preMuBenfitEvaluationChatService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuBenfitEvaluationChatService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < cList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(cList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(cList.get(i).getCIndexNum());
                    }
                }
            }

        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
            List<PreMuChartsVo> sList=    preMuBenfitEvaluationChatService.groupEconomyByCityAndCounty(preChartsDto);
            List<PreMuChartsVo> pList=  preMuBenfitEvaluationChatService.groupEconomyByProvince(preChartsDto);
            for (int i = 0; i < pList.size(); i++) {
                PreMuChartsVo preMuChartsVo=new PreMuChartsVo();
                preMuChartsVo.setYears(pList.get(i).getYears());
                preMuChartsVo.setPIndexNum(pList.get(i).getPIndexNum());
                chartsVoList.add(preMuChartsVo);
            }
            for (int i = 0; i < sList.size(); i++) {
                for (int j = 0; j < chartsVoList.size(); j++) {
                    if(sList.get(i).getYears().contains(chartsVoList.get(j).getYears())){
                        chartsVoList.get(j).setCIndexNum(sList.get(i).getCIndexNum());
                    }
                }
            }
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
            chartsVoList=  preMuBenfitEvaluationChatService.groupEconomyByProvince(preChartsDto);
        }else if(roles.contains("admin")){
            preChartsDto.setProvince("370000");
            chartsVoList=  preMuBenfitEvaluationChatService.groupEconomyByProvince(preChartsDto);
        }
        return  success(chartsVoList);
    }


    @GetMapping(value = "/economyGruopArea")
    public AjaxResult economyGruopArea(PreMuChartsDto preChartsDto) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province);
        }else if(roles.contains("admin")){

            preChartsDto.setProvince("370000");
        }

        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.economyGruopArea(preChartsDto);

        BigDecimal sum = null;
        if(chartsVoList!=null&&chartsVoList.size()>0){
            sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
            for (int i = 0; i < chartsVoList.size(); i++) {
                BigDecimal  indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
                if(sum.compareTo(BigDecimal.valueOf(0))!=0){
                    BigDecimal result=indexNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                    chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
                }else{
                    chartsVoList.get(i).setProportionLevel("0");
                }
            }
        }
        return  success(PageUtils.paginateList(chartsVoList,preChartsDto.getPageNum(), preChartsDto.getPageSize()) );
    }

    @GetMapping(value = "/economyGruopIndustry")
    public AjaxResult economyGruopIndustry(PreMuChartsDto preChartsDto) {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();
        if(roles.contains("x_01_011")||roles.contains("along_yellow_river")){
            String county=  loginUser.getSysUser().getDistrict();
            preChartsDto.setCounty(county);
        }else if(roles.contains("s_01_011")){
            String city=  loginUser.getSysUser().getCity();
            preChartsDto.setCity(city);
        }else if(roles.contains("p_01_011")){
            String province=  loginUser.getSysUser().getProvince();
            preChartsDto.setProvince(province); 
        }else if(roles.contains("admin")){

            preChartsDto.setProvince("370000");
        }
        preChartsDto.setPageNum(0);
        preChartsDto.setPageSize(10);

        List<PreMuChartsVo> chartsVoList= preMuBenefitEvaluationService.economyGruopIndustry(preChartsDto);
        BigDecimal sum = null;
        BigDecimal bili = new BigDecimal("0");
        if(chartsVoList!=null&&chartsVoList.size()>0){
            sum = chartsVoList.stream().map(i -> new BigDecimal(i.getIndexNum())).reduce(BigDecimal::add).get();
            for (int i = 0; i < chartsVoList.size(); i++) {
                BigDecimal  indexNum=new BigDecimal(chartsVoList.get(i).getIndexNum());
                if(sum.compareTo(BigDecimal.ZERO)!=0){
                    BigDecimal result=indexNum.divide(sum,2,BigDecimal.ROUND_HALF_UP);
                    chartsVoList.get(i).setProportionLevel(String.valueOf(result.multiply(new BigDecimal(100))));
                    bili=bili.add(result.multiply(new BigDecimal(100)));
                }

            }
        }
        preChartsDto.setPageNum(1);
        return success(PageUtils.paginateList(chartsVoList,preChartsDto.getPageNum(), preChartsDto.getPageSize()) );
    }

}
